package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/clues")
public class ClueController {
    @Autowired
    private ClueService clueService;

    @PostMapping
    public Result add(@RequestBody Clue clue){
        clue.setStatus(1);
        clue.setUpdateTime(LocalDateTime.now());
        clue.setCreateTime(LocalDateTime.now());
        clueService.save( clue);
        return Result.success();
    }


    @GetMapping
    public Result List(ClueDto clueDto){
        PageResult <Clue> list =clueService.selectList(clueDto);
        return Result.success(list);
    }

    @PutMapping("/assign/{clueId}/{userId}")
    public Result assign(@PathVariable Integer clueId,@PathVariable Integer userId){
        log.info("修改线索,id={},userId={}",clueId,userId);
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setUserId(userId);
        clue.setStatus(2);
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selsctById(@PathVariable Integer id){
        log.info("查询线索{}",id);
        Clue clue =clueService.selectClById(id);
        return Result.success(clue);
    }

    @PutMapping
    public Result update(@RequestBody Clue  clue){
            log.info("修改线索{}",clue);
            clueService.updateTrById(clue);
            return Result.success();
    }

    @PutMapping("/false/{id}")
    public Result update(@PathVariable Integer id,@RequestBody ClueDto clueDto ){
            log.info("修改线索{}",id);
            clueService.updateFalseById(id , clueDto);
            return Result.success();
    }

}
